package cn.ivicar.iov.service.impl;

import cn.ivicar.iov.dao.IdentifyDao;
import cn.ivicar.iov.dao.SimInfoDao;
import cn.ivicar.iov.dao.VehicleDao;
import cn.ivicar.iov.enums.ResultEnum;
import cn.ivicar.iov.exception.IovException;
import cn.ivicar.iov.service.SimInfoService;
import cn.ivicar.iov.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Chen
 * @Description:
 * @Date: Created in 17:54 2018/8/20
 */
@Service
public class SimInfoServiceImpl implements SimInfoService  {

    @Autowired
    private VehicleDao vehicleDao;
    @Autowired
    private IdentifyDao identifyDao;
    @Autowired
    private SimInfoDao simInfoDao;

    /*
    获取sim卡信息
     */
    @Override
    public Map getSimInfo(String vehicleSerial) {
        Map<String,Object> map = new HashMap<>();
        String iccid;
        Integer vehicleId = vehicleDao.queryVehicleId(vehicleSerial);
//        if (vehicleId !=null) {
//            iccid = identifyDao.queryByVehicleId(vehicleId);//得到iccid
//        }else {
//            throw new IovException(ResultEnum.DATE_NULL);//没有此
//        }
        iccid = identifyDao.queryByVehicleId(vehicleId);//得到iccid
        System.out.println(iccid);
        if(iccid==null||"".equals(iccid)){
            throw new IovException(ResultEnum.DATE_NULL);//没有此iccid
        }
        map.put("licensePlate",vehicleDao.queryLicensePlate(vehicleSerial));//车牌号
        map.put("vehicleId",vehicleDao.queryVehicleId(vehicleSerial));//设备ID
        map.put("flowRemain",simInfoDao.queryFlowRemainByIccid(iccid));//流量剩余
        map.put("deadline",simInfoDao.queryDeadlineByIccid(iccid));//截止日期
        map.put("cardStatus",simInfoDao.queryCardStatusByIccid(iccid));//卡状态
        return map;
    }

    /*
    导出注销卡信息
     */
    @Override
    public void outputData(HttpServletResponse httpServletResponse) {
        httpServletResponse.setContentType("application/vnd.ms-excel;charset=utf-8");
        httpServletResponse.setHeader("Content-Disposition","attachment;filename=logoutIccid.xls");
        try {
            OutputStream os = httpServletResponse.getOutputStream();
            OutputUtil.getExcel(simInfoDao.queryLogoutIccid()).write(os);
            os.flush();
            os.close();
        }catch (IOException e){
            e.printStackTrace();
        }
        return;

    }
}
